//
//  FaQiShenPiViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/22.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "FaQiShenPiViewController.h"
#import "ShenPiXiangQingShenPiRenTableViewCell.h"
#import "ChaKanQuanBuViewController.h"
#import "XinJianKeHuWenBenYuTableViewCell.h"
#import "FaQiShenPiTuPianTableViewCell.h"
#import "FaQiShenPiFuJianTableViewCell.h"
#import "FaQiShenPiGuanLianKeHuTableViewCell.h"
#import "FaQiShenPiXuanChengYuanViewController.h"
#import "NSDate+Helper.h"
#import "XLPhotoBrowser.h"
#import "LiaoTianWenJianChaKanViewController.h"
#import "YuYinBoFangView.h"
#import "ZBNetworking.h"
#import "ShenPiXiangQingShenPiRenTableViewCell.h"

#import "UITextView+WZB.h"
#import "XinJianKeHuTableViewCell.h"

#import "BRStringPickerView.h"
#import "BRAddressPickerView.h"
#import "BRDatePickerView.h"

@interface FaQiShenPiViewController ()<UITableViewDelegate,UITableViewDataSource,UIDocumentPickerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>{
    NSMutableArray *shuRuArray;
    NSArray *chengYuanArray;
    NSMutableArray *imageArray;
    NSMutableArray *fileArray;
    NSString *batchId;
    
    NSString *guanLianName;
    NSString *guanLianId;
    
    YuYinBoFangView *yuYinView;
    
    NSString *address;
}
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *mainTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@property (nonatomic,strong) NSArray *addressSelectIndexs;


@end

@implementation FaQiShenPiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.customNavBar.title = @"发起审批";
    _topSpace.constant = NavaBarHeight;
    
    _bottomSpace.constant = ScreenBottom+20;
    
    if (@available(iOS 11.0, *)) {
        self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"ShenPiXiangQingShenPiRenTableViewCell" bundle:nil] forCellReuseIdentifier:@"ShenPiXiangQingShenPiRenTableViewCell"];
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"XinJianKeHuWenBenYuTableViewCell" bundle:nil] forCellReuseIdentifier:@"XinJianKeHuWenBenYuTableViewCell"];
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"FaQiShenPiTuPianTableViewCell" bundle:nil] forCellReuseIdentifier:@"FaQiShenPiTuPianTableViewCell"];
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"FaQiShenPiFuJianTableViewCell" bundle:nil] forCellReuseIdentifier:@"FaQiShenPiFuJianTableViewCell"];
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"FaQiShenPiGuanLianKeHuTableViewCell" bundle:nil] forCellReuseIdentifier:@"FaQiShenPiGuanLianKeHuTableViewCell"];
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"XinJianKeHuTableViewCell" bundle:nil] forCellReuseIdentifier:@"XinJianKeHuTableViewCell"];
    
    
    
    
    imageArray = [NSMutableArray array];
    [imageArray addObject:[NSDictionary dictionary]];
    
    fileArray = [NSMutableArray array];
    [fileArray addObject:[NSDictionary dictionary]];
    
    [self huoQuBeiZhu];
    
    [self huoQuShenPiRen];
    
    
    NSString *time = [NSString stringWithFormat:@"%@%@%@%@",[ZZPUtils getTimeStamp],[ZZPUtils getTimeStamp],[ZZPUtils getTimeStamp],[ZZPUtils getTimeStamp]];
    
    batchId = [time substringToIndex:32];
    
    yuYinView = [YuYinBoFangView view];
    [[AppDelegate shareInstance].window addSubview:yuYinView];
  
    yuYinView.frame = CGRectMake(0, ScreenHeight-107-30, ScreenWidth, 107);
    yuYinView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    yuYinView.hidden = YES;
    [yuYinView closeVoice];
}

//新建查询信息 审批事由 备注
- (void)huoQuBeiZhu{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:@"10" forKey:@"label"];
    NSString *url = [NSString stringWithFormat:@"/oaExamineField/queryField/%@",_examineId];
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:url andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            shuRuArray = [NSMutableArray array];
            NSArray *array = [info objectForKey:@"data"];
            for (NSInteger i = 0; i < array.count; i++) {
                NSArray *tempArray = array[i];
                for (NSInteger j = 0; j < tempArray.count; j++) {
                    [shuRuArray addObject:tempArray[j]];
                }
            }
            [_mainTableView reloadData];
        }
    }];
}

//新建查询信息 审核信息审批人
- (void)huoQuShenPiRen{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:_label forKey:@"label"];
    [paraDic setValue:_examineId forKey:@"examineId"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/examines/previewExamineFlow" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSDictionary *data = [info objectForKey:@"data"];
            chengYuanArray = [data objectForKey:@"examineFlowList"];
            [_mainTableView reloadData];
        }
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        NSDictionary *dataDic = shuRuArray[indexPath.row];
        if([GetHandleString(dataDic, @"type") integerValue] == 2){
            return 162;
        }
        return 102;
    }
    if(indexPath.section == 1){
        AllModel *model = [[AllModel alloc] init];
        model.imageArray = imageArray;
        return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[FaQiShenPiTuPianTableViewCell class] contentViewWidth:[self cellContentViewWith]];
    }
    if(indexPath.section == 2){
        AllModel *model = [[AllModel alloc] init];
        model.imageArray = fileArray;
        return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[FaQiShenPiFuJianTableViewCell class] contentViewWidth:[self cellContentViewWith]];
    }
    if(indexPath.section == 3){
        return 164;
    }
    AllModel *model = [[AllModel alloc] init];
    model.modelInfoDictionary = chengYuanArray[indexPath.row];
 
    return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[ShenPiXiangQingShenPiRenTableViewCell class] contentViewWidth:[self cellContentViewWith]];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return shuRuArray.count;
    }
    if(section == 4){
        return chengYuanArray.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithDictionary:shuRuArray[indexPath.row]];

//        `type` int(11) NOT NULL DEFAULT '1' COMMENT '字段类型 1 单行文本 2 多行文本 3 单选 4日期 5 数字 6 小数 7 手机  8 文件 9 多选 10 人员 11 附件 12 部门 13 日期时间 14 邮箱 15客户 16 商机 17 联系人 18 地图 19 产品类型 20 合同 21 回款计划',
        
        
        
        if([GetHandleString(dataDic, @"type") integerValue] == 2){

            XinJianKeHuWenBenYuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XinJianKeHuWenBenYuTableViewCell"];
            cell.titleLabel.text = GetHandleString(dataDic, @"name");
            if ([GetHandleString(dataDic, @"isNull") integerValue] == 1) {
                cell.readRemindLabel.text = @"*";
            }else{
                cell.readRemindLabel.text = @"";
            }

            cell.insertTextView.wzb_placeholder = [NSString stringWithFormat:@"请输入%@",GetHandleString(dataDic, @"name")];

            cell.insertTextView.text = GetHandleString(dataDic, @"value");
            cell.numLabel.text = [NSString stringWithFormat:@"%ld/%@",[GetHandleString(dataDic, @"value") length],GetHandleString(dataDic, @"maxLength")];
            cell.maxLength = [GetHandleString(dataDic, @"maxLength") integerValue];
            
            cell.textViewEndEditingBlock = ^(NSString * _Nonnull result) {
                [dataDic setValue:result forKey:@"value"];
                [shuRuArray replaceObjectAtIndex:indexPath.row withObject:dataDic];
                [UIView performWithoutAnimation:^{
                    [_mainTableView reloadData];
                }];
            };
            
            return cell;
        }
        
        XinJianKeHuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XinJianKeHuTableViewCell"];
        cell.titleLabel.text = GetHandleString(dataDic, @"name");
      
        cell.insertTextView.text = GetHandleString(dataDic, @"value");
        
        
//        `type` int(11) NOT NULL DEFAULT '1' COMMENT '字段类型 1 单行文本 2 多行文本 3 单选 4日期 5 数字 6 小数 7 手机  8 文件 9 多选 10 人员 11 附件 12 部门 13 日期时间 14 邮箱 15客户 16 商机 17 联系人 18 地图 19 产品类型 20 合同 21 回款计划',
        
        if([GetHandleString(dataDic, @"type") integerValue] == 7){
            cell.insertTextView.keyboardType = UIKeyboardTypeNamePhonePad;
        }else if([GetHandleString(dataDic, @"type") integerValue] == 6){
            cell.insertTextView.keyboardType = UIKeyboardTypeDecimalPad;
        }else{
            cell.insertTextView.keyboardType = UIKeyboardTypeDefault;
        }
        
        if([GetHandleString(dataDic, @"type") integerValue] == 3 || [GetHandleString(dataDic, @"type") integerValue] == 4 || [GetHandleString(dataDic, @"type") integerValue] == 13 || [GetHandleString(dataDic, @"type") integerValue] == 43){
            cell.xuanZeButton.hidden = NO;
            cell.rightSpace.constant = 64;
            cell.insertTextView.placeholder = @"请选择";
            cell.insertTextView.userInteractionEnabled = NO;
        }else{
            cell.xuanZeButton.hidden = YES;
            cell.rightSpace.constant = 16;
            cell.insertTextView.placeholder = [NSString stringWithFormat:@"请输入%@",GetHandleString(dataDic, @"name")];
            cell.insertTextView.userInteractionEnabled = YES;
        }
        
        if ([GetHandleString(dataDic, @"isNull") integerValue] == 1) {
            cell.readRemindLabel.text = @"*";
        }else{
            cell.readRemindLabel.text = @"";
        }
        
        cell.insertTextView.delegate = self;
        cell.insertTextView.tag = indexPath.row*100;
        return cell;
    }
    if(indexPath.section == 1){
        FaQiShenPiTuPianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FaQiShenPiTuPianTableViewCell"];
        AllModel *model = [[AllModel alloc] init];
        model.imageArray = imageArray;
        cell.model = model;
        
        cell.manageClickBlock = ^(NSDictionary * _Nonnull resultDic, NSInteger manageType) {
            if(manageType == 2){
//                添加更多
                [self shangChuanTuPian];
            }else if(manageType == 0){
                //                删除
                [self shanChuWenJianWithDic:resultDic andIsImage:YES];
            }else if(manageType == 1){
                //                预览
         
                [XLPhotoBrowser showPhotoBrowserWithImages:@[GetHandleString(resultDic, @"urlImage")] currentImageIndex:0];
                
            }
        };
        return cell;
    }
    
    if(indexPath.section == 2){
        FaQiShenPiFuJianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FaQiShenPiFuJianTableViewCell"];
        AllModel *model = [[AllModel alloc] init];
        model.imageArray = fileArray;
        cell.model = model;
        cell.manageClickBlock = ^(NSDictionary * _Nonnull resultDic, NSInteger manageType) {
            if(manageType == 2){
                //                添加更多
                [self selectiCloudFile];
            }else if(manageType == 0){
                //                删除
                [self shanChuWenJianWithDic:resultDic andIsImage:NO];
            }else if(manageType == 1){
                //                预览
                if([GetHandleString(resultDic, @"urlImage") containsString:@".mp3"]){
                    yuYinView.hidden = NO;
                    yuYinView.titleLabel.text = GetHandleString(resultDic, @"name");
                    yuYinView.voiceUrl = GetHandleString(resultDic, @"urlImage");
                }else{
                    LiaoTianWenJianChaKanViewController *yuLan = [[LiaoTianWenJianChaKanViewController alloc] init];
                    yuLan.loadUrl = GetHandleString(resultDic, @"urlImage");
                    [self pushViewController:yuLan];
                }
                
            }
        };
        return cell;
    }
    
    if(indexPath.section == 3){
        FaQiShenPiGuanLianKeHuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FaQiShenPiGuanLianKeHuTableViewCell"];
        cell.keHuTf.text = guanLianName;
        cell.xuanKeHuClickBlock = ^{
            [self.view endEditing:YES];
            FaQiShenPiXuanChengYuanViewController *xuan = [[FaQiShenPiXuanChengYuanViewController alloc] init];
            xuan.manageClickResultBlock = ^(NSString * _Nonnull resultName, NSString *resultId) {
                guanLianId = resultId;
                guanLianName = resultName;
                [UIView performWithoutAnimation:^{
                    [_mainTableView reloadData];
                }];
            };
            [self pushViewController:xuan];
        };
        return cell;
    }
    
    
    ShenPiXiangQingShenPiRenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShenPiXiangQingShenPiRenTableViewCell"];

    NSDictionary *dataDic = chengYuanArray[indexPath.row];
    AllModel *model = [[AllModel alloc] init];
    model.modelInfoDictionary = dataDic;
    cell.model = model;
    return cell;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSInteger row = textField.tag/100;
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithDictionary:shuRuArray[row]];

    if([GetHandleString(dataDic, @"type") integerValue] == 7){
        // 获取当前文本框中的文本
        NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        
        // 设置最大字符长度
        NSUInteger maxLength = 11; // 例如限制输入长度为10个字符
        
        // 检查是否超过最大长度
        if (newText.length <= maxLength) {
            return YES; // 允许输入
        } else {
            return NO; // 超过最大长度，禁止输入
        }
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    
    NSInteger row = textField.tag/100;
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithDictionary:shuRuArray[row]];
    
    [dataDic setValue:textField.text forKey:@"value"];
 
    [shuRuArray replaceObjectAtIndex:row withObject:dataDic];
    [_mainTableView reloadData];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.view endEditing:YES];
    
    //        `type` int(11) NOT NULL DEFAULT '1' COMMENT '字段类型 1 单行文本 2 多行文本 3 单选 4日期 5 数字 6 小数 7 手机  8 文件 9 多选 10 人员 11 附件 12 部门 13 日期时间 14 邮箱 15客户 16 商机 17 联系人 18 地图 19 产品类型 20 合同 21 回款计划',
            
            
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithDictionary:shuRuArray[indexPath.row]];
    
    if([GetHandleString(dataDic, @"type") integerValue] == 3){
        BRStringPickerView *datePickerView = [[BRStringPickerView alloc]init];
        datePickerView.pickerMode = BRStringPickerComponentSingle;
        datePickerView.title = @"";
        datePickerView.dataSourceArr = [dataDic objectForKey:@"setting"];
        datePickerView.selectValue = GetHandleString(dataDic, @"value");
        datePickerView.resultModelBlock = ^(BRResultModel *resultModel) {
            NSLog(@"选择的索引：%@", @(resultModel.index));
            NSLog(@"选择的值：%@", resultModel.value);
            
            [dataDic setValue:resultModel.value forKey:@"value"];
            
            [shuRuArray replaceObjectAtIndex:indexPath.row withObject:dataDic];
            [_mainTableView reloadData];
            
        };
        
        BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
        customStyle.pickerColor = [UIColor whiteColor];
        customStyle.selectRowTextColor = rgba(40, 120, 255, 1);
        customStyle.titleBarColor = [UIColor whiteColor];
        customStyle.titleTextColor = [UIColor whiteColor];
        customStyle.cancelTextColor = rgba(153, 153, 153, 1);
        customStyle.doneTextColor = rgba(40, 120, 255, 1);
        customStyle.titleBarHeight = 48;
        customStyle.titleTextFont = [UIFont systemFontOfSize:18];
        datePickerView.pickerStyle = customStyle;
        customStyle.topCornerRadius = 10;
        customStyle.hiddenTitleLine = YES;
        customStyle.separatorColor = rgba(223, 229, 242, 1);
        customStyle.separatorHeight = 0.5;
        customStyle.rowHeight = 64;
        
        [datePickerView show];
        
        
    }else if([GetHandleString(dataDic, @"type") integerValue] == 43){
        BRAddressPickerView *addressPickerView = [[BRAddressPickerView alloc]init];
        addressPickerView.pickerMode = BRAddressPickerModeArea;
        addressPickerView.title = @"";
        addressPickerView.isAutoSelect = NO;
        addressPickerView.selectIndexs = self.addressSelectIndexs;
        addressPickerView.resultBlock = ^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
            self.addressSelectIndexs = @[@(province.index), @(city.index), @(area.index)];
            address = [NSString stringWithFormat:@"%@%@%@",province.name,city.name,area.name];
//            [_mainTableView reloadData];
            
            
            [dataDic setValue:address forKey:@"value"];
            
            [shuRuArray replaceObjectAtIndex:indexPath.row withObject:dataDic];
            [_mainTableView reloadData];
            
        };
        BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
        customStyle.pickerColor = [UIColor whiteColor];
        customStyle.selectRowTextColor = rgba(51, 51, 51, 1);
        customStyle.titleBarColor = [UIColor whiteColor];
        customStyle.titleTextColor = [UIColor whiteColor];
        customStyle.cancelTextColor = rgba(153, 153, 153, 1);
        customStyle.doneTextColor = rgba(40, 120, 255, 1);
        customStyle.titleBarHeight = 48;
        customStyle.titleTextFont = [UIFont systemFontOfSize:18];
        addressPickerView.pickerStyle = customStyle;
        customStyle.topCornerRadius = 10;
        customStyle.hiddenTitleLine = YES;
        customStyle.separatorColor = rgba(223, 229, 242, 1);
        customStyle.separatorHeight = 0.5;
        customStyle.rowHeight = 64;
        
        [addressPickerView show];
    }else if([GetHandleString(dataDic, @"type") integerValue] == 13 || [GetHandleString(dataDic, @"type") integerValue] == 4){
        BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
        datePickerView.pickerMode = BRDatePickerModeDate;
        datePickerView.title = @"";
        datePickerView.minDate = nil;
        datePickerView.maxDate = nil;
        datePickerView.isAutoSelect = NO;

        datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
       
            NSLog(@"selectValue=%@", selectValue);
            NSLog(@"selectDate=%@", selectDate);
            NSLog(@"---------------------------------");
            [dataDic setValue:selectValue forKey:@"value"];
            
            [shuRuArray replaceObjectAtIndex:indexPath.row withObject:dataDic];
            [_mainTableView reloadData];
        };
     
        
        BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
        customStyle.pickerColor = [UIColor whiteColor];
        customStyle.selectRowTextColor = rgba(51, 51, 51, 1);
        customStyle.titleBarColor = [UIColor whiteColor];
        customStyle.titleTextColor = [UIColor whiteColor];
        customStyle.cancelTextColor = rgba(153, 153, 153, 1);
        customStyle.doneTextColor = rgba(40, 120, 255, 1);
        customStyle.titleBarHeight = 48;
        customStyle.titleTextFont = [UIFont systemFontOfSize:18];
        datePickerView.pickerStyle = customStyle;
        customStyle.topCornerRadius = 10;
        customStyle.hiddenTitleLine = YES;
        customStyle.separatorColor = rgba(223, 229, 242, 1);
        customStyle.separatorHeight = 0.5;
        customStyle.rowHeight = 64;

        [datePickerView show];
    }


    
}
//删除文件
- (void)shanChuWenJianWithDic:(NSDictionary *)dataDic andIsImage:(BOOL)isImage{
    NSString *url = [NSString stringWithFormat:@"/adminFile/deleteById/%@",GetHandleString(dataDic, @"fileId")];
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:url andParas:nil andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            if (isImage) {
                [imageArray removeObject:dataDic];
                NSDictionary *dataDic = imageArray.lastObject;
                if([[dataDic allValues] count] > 0){
                    [imageArray addObject:[NSDictionary dictionary]];
                }
            }else{
                [fileArray removeObject:dataDic];
                NSDictionary *dataDic = fileArray.lastObject;
                if([[dataDic allValues] count] > 0){
                    [fileArray addObject:[NSDictionary dictionary]];
                }
            }
            [UIView performWithoutAnimation:^{
                [_mainTableView reloadData];
            }];
        }
    }];
}


//上传图片
- (void)shangChuanTuPian{
    
    [self.view endEditing:YES];
    
    SPAlertController *alertController = [SPAlertController alertControllerWithTitle:nil message:nil preferredStyle:SPAlertControllerStyleActionSheet];
    SPAlertAction *action1 = [SPAlertAction actionWithTitle:@"拍摄" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
         //拍照
         [self takePhoto];
    }];
    [alertController addAction:action1];
    action1.titleColor = mainFontColor;
    action1.titleFont = [UIFont systemFontOfSize:17];
    SPAlertAction *action2 = [SPAlertAction actionWithTitle:@"从相册选择" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {
        //相册
        [self LocalPhoto];
    }];
    [alertController addAction:action2];
    action2.titleColor = mainFontColor;
    action2.titleFont = [UIFont systemFontOfSize:17];
    SPAlertAction *cancel = [SPAlertAction actionWithTitle:@"取消" style:SPAlertActionStyleCancel handler:^(SPAlertAction * _Nonnull action) {
    }];
    [alertController addAction:cancel];
    cancel.titleColor = mainFontColor;
    cancel.titleFont = [UIFont systemFontOfSize:17];
    [self presentViewController:alertController animated:YES completion:nil];
}


//上传文件
- (void)shangChuanWenJianWithParaDic:(NSDictionary *)paraDic andIsImage:(BOOL)isImage andDataArray:(NSArray *)dataArray{
    [[ZZPRequest getWebUtil] uploadImageRequestURLString:@"/adminFile/upload" andParas:paraDic andImageArray:dataArray andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSDictionary *data = [info objectForKey:@"data"];
            if(isImage){
                [imageArray removeLastObject];
                [imageArray addObject:data];
                if(imageArray.count < 9){
                    [imageArray addObject:[NSDictionary dictionary]];
                }
            }else{
                [fileArray removeLastObject];
                [fileArray addObject:data];
                if(fileArray.count < 9){
                    [fileArray addObject:[NSDictionary dictionary]];
                }
            }
            [UIView performWithoutAnimation:^{
                [_mainTableView reloadData];
            }];
        }
    }];
}

//开始拍照
- (void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//        [[picker navigationBar] setBarTintColor:[UIColor whiteColor]];
        picker.delegate = self;
        picker.allowsEditing = NO;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:^{
            self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

        }];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"])
    {
        UIImage * image;
        if (picker.allowsEditing) {
            image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        }else
        {
            image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        }
        NSData * imageData = UIImageJPEGRepresentation(image, 0.05);

        NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
        [tempDic setValue:imageData forKey:@"data"];
        [tempDic setValue:[NSString stringWithFormat:@"%f.png",[[NSDate date] timeStamp]] forKey:@"name"];
        NSArray *array = @[tempDic];
        
        NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
        [paraDic setValue:@"img" forKey:@"type"];
        [paraDic setValue:batchId forKey:@"batchId"];
        
        [self shangChuanWenJianWithParaDic:paraDic andIsImage:YES andDataArray:array];
        
        
        
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

}
//打开本地相册
- (void)LocalPhoto{
    NSInteger maxNum = 10 - imageArray.count;
    TZImagePickerController *imagePickerVc=[[TZImagePickerController alloc] initWithMaxImagesCount:maxNum columnNumber:4 delegate:nil pushPhotoPickerVc:YES];

    imagePickerVc.oKButtonTitleColorNormal = rgba(24, 27, 37, 1);
    imagePickerVc.naviBgColor = [UIColor whiteColor];
    imagePickerVc.naviTitleColor = rgba(24, 27, 37, 1);
    imagePickerVc.barItemTextColor = rgba(24, 27, 37, 1);
    
//    imagePickerVc.selectedAssets=_selectAsssts;
    imagePickerVc.selectedAssets=nil;
    //不允许 选择 视频和gif 动图 如需要 自行设置
    imagePickerVc.allowPickingVideo=NO;
    imagePickerVc.allowPickingGif=NO;
    imagePickerVc.allowPreview=NO;
   
    imagePickerVc.allowCrop = YES;

    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {

        for (UIImage *img in photos) {
            
            NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
            [tempDic setValue:UIImageJPEGRepresentation(img, 0.5) forKey:@"data"];
            [tempDic setValue:[NSString stringWithFormat:@"%f.png",[[NSDate date] timeStamp]] forKey:@"name"];
            NSArray *array = @[tempDic];
            
            NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
            [paraDic setValue:@"img" forKey:@"type"];
            [paraDic setValue:batchId forKey:@"batchId"];
            
            [self shangChuanWenJianWithParaDic:paraDic andIsImage:YES andDataArray:array];
            
        }

    }];
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}


#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
    //获取授权
    BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
    if (fileUrlAuthozied) {
        //通过文件协调工具来得到新的文件地址，以此得到文件保护功能
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;
        
        [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
            //读取文件
            NSString *fileName1 = [newURL lastPathComponent];
            NSError *error = nil;
            NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
            if (error) {
                //读取出错
            } else {
                //文件 上传或者其它操作
//                [self uploadingWithFileData:fileData fileName:fileName fileURL:newURL];
                
                NSLog(@"------------->文件 上传或者其它操作");
                
                long long currentTime = [[NSDate date] timeIntervalSince1970] * 1000;

                NSString *fileName = [NSString stringWithFormat:@"/file_%@/%@", @(currentTime),fileName1];

                NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
                [tempDic setValue:fileData forKey:@"data"];
                [tempDic setValue:fileName forKey:@"name"];
                NSArray *array = @[tempDic];
                
                NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
                [paraDic setValue:@"file" forKey:@"type"];
                [paraDic setValue:batchId forKey:@"batchId"];
                
                [self shangChuanWenJianWithParaDic:paraDic andIsImage:NO andDataArray:array];

            }
            [self dismissViewControllerAnimated:YES completion:NULL];
        }];
        [urls.firstObject stopAccessingSecurityScopedResource];
    } else {
        //授权失败
    }
}
- (void)selectiCloudFile{
    [self.view endEditing:YES];
    NSArray *documentTypes = @[@"public.content", @"public.text", @"public.source-code", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt"];
    UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
    documentPickerViewController.delegate = self;
    documentPickerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:documentPickerViewController animated:YES completion:nil];
}

- (void)fileDidSelect:(NSArray *)filePathList{
    
}
//发起审批
- (IBAction)faQiShenPi:(UIButton *)sender {
    [self.view endEditing:YES];
    
    NSDictionary *remark = [self huoQuHuoJuByFieldName:@"remark"];
    if([GetHandleString(remark, @"isNull") integerValue] == 1 && [GetHandleString(remark, @"value") length] == 0){
        [ZZPUtils showWarning:[NSString stringWithFormat:@"请输入%@",GetHandleString(remark, @"name")]];
        return;
    }
    
    NSDictionary *content = [self huoQuHuoJuByFieldName:@"content"];
    if([GetHandleString(remark, @"isNull") integerValue] == 1 && [GetHandleString(remark, @"value") length] == 0){
        [ZZPUtils showWarning:[NSString stringWithFormat:@"请输入%@",GetHandleString(remark, @"name")]];
        return;
    }
    
//    if(guanLianId.length == 0){
//        [ZZPUtils showWarning:@"请先选择关联客户"];
//        return;
//    }
    
    
    //        `type` int(11) NOT NULL DEFAULT '1' COMMENT '字段类型 1 单行文本 2 多行文本 3 单选 4日期 5 数字 6 小数 7 手机  8 文件 9 多选 10 人员 11 附件 12 部门 13 日期时间 14 邮箱 15客户 16 商机 17 联系人 18 地图 19 产品类型 20 合同 21 回款计划',
    for (NSDictionary *dataDic in shuRuArray) {
        if([GetHandleString(dataDic, @"type") integerValue] == 3 || [GetHandleString(dataDic, @"type") integerValue] == 4 || [GetHandleString(dataDic, @"type") integerValue] == 13 || [GetHandleString(dataDic, @"type") integerValue] == 43){
            if([GetHandleString(dataDic, @"isNull") integerValue] == 1 && [GetHandleString(dataDic, @"value") length] == 0){
                [ZZPUtils showWarning:[NSString stringWithFormat:@"请选择%@",GetHandleString(dataDic, @"name")]];
                return;
            }
        }else{
            if([GetHandleString(dataDic, @"isNull") integerValue] == 1 && [GetHandleString(dataDic, @"value") length] == 0){
                [ZZPUtils showWarning:[NSString stringWithFormat:@"请输入%@",GetHandleString(dataDic, @"name")]];
                return;
            }
        }
    }
    
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    
    NSMutableDictionary *oaExamine = [NSMutableDictionary dictionary];
    [oaExamine setValue:_examineId forKey:@"categoryId"];
    [oaExamine setValue:GetHandleString(content, @"value") forKey:@"content"];
    [oaExamine setValue:GetHandleString(remark, @"value") forKey:@"remark"];
    [oaExamine setValue:batchId forKey:@"batchId"];
    [paraDic setValue:oaExamine forKey:@"oaExamine"];
    
    
    NSMutableDictionary *oaExamineRelation = [NSMutableDictionary dictionary];
    [oaExamineRelation setValue:guanLianId forKey:@"customerIds"];
    [oaExamineRelation setValue:@"" forKey:@"contactsIds"];
    [oaExamineRelation setValue:@"" forKey:@"businessIds"];
    [oaExamineRelation setValue:@"" forKey:@"contractIds"];
    [paraDic setValue:oaExamineRelation forKey:@"oaExamineRelation"];

    NSMutableArray *field = [NSMutableArray array];
    for (NSDictionary *dataDic in shuRuArray) {
        NSMutableDictionary *tempD = [NSMutableDictionary dictionary];
        [tempD setValue:GetHandleString(dataDic, @"fieldId") forKey:@"fieldId"];
        [tempD setValue:GetHandleString(dataDic, @"fieldName") forKey:@"fieldName"];
        [tempD setValue:GetHandleString(dataDic, @"fieldType") forKey:@"fieldType"];
        [tempD setValue:GetHandleString(dataDic, @"name") forKey:@"name"];
        [tempD setValue:GetHandleString(dataDic, @"type") forKey:@"type"];
        [tempD setValue:GetHandleString(dataDic, @"value") forKey:@"value"];
        [field  addObject:tempD];
    }
    [paraDic setValue:field forKey:@"field"];
    
    [paraDic setValue:@[] forKey:@"oaExamineTravelList"];
    
    NSMutableDictionary *examineFlowData = [NSMutableDictionary dictionary];
    [examineFlowData setValue:_label forKey:@"label"];
    [examineFlowData setValue:_examineId forKey:@"examineId"];
    
    [examineFlowData setValue:@{} forKey:@"dataMap"];
    
    [examineFlowData setValue:@[] forKey:@"optionalList"];
    
    [paraDic setValue:examineFlowData forKey:@"examineFlowData"];
    
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/oaExamine/setOaExamine" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            [ZZPUtils showWarning:@"创建成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (NSDictionary *)huoQuHuoJuByFieldName:(NSString *)fieldName{
    NSDictionary *dataDic = [NSDictionary dictionary];
    for (NSDictionary *temp in shuRuArray) {
        if([GetHandleString(temp, @"fieldName") isEqualToString:fieldName]){
            dataDic = temp;
            break;
        }
    }
    return dataDic;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
